var index=1;
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/flow/index/index',
                    add_url: 'facrm/flow/index/add',
                    edit_url: 'facrm/flow/index/edit',
                    del_url: 'facrm/flow/index/del',
                    multi_url: '',
                    table: 'facrm_flow',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('审批名称'), operate: "LIKE"},
                        {field: 'types', title: __('审批类型'),
                            formatter: Table.api.formatter.status,
                            searchList: {'contract': __('合同'), 'receivables': __('回款'), 'invoice': __('发票')}},
							
						{field: 'department_ids', title: __('适用部门') ,
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="facrm/common/group" data-field="name"',
                            operate: 'find_in_set',
                            formatter: Table.api.formatter.search},
                        {field: 'groups', title: __('适用部门') ,
                            operate:false,
                            formatter: function (value, row, index) {
                            console.log(value);
                                if (!value||value.length == 0)
                                    return '通用' ;
                                var group="";
                                $.each(value,function(i,v){  //arrTmp数组数据
                                    group+=group?','+v:v;
                                });
                                return  Table.api.formatter.label(group);
                            }
                            },
                        {field: 'remark', title: __('备注'), operate: 'LIKE'},
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
						 {
                            field: 'weigh',
                            title: __('权重'),
                            sortable: true
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {0: __('禁用'), 1: __('启用')}
                        },
						
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $(document).on('click', '.addRow',function(){
                Controller.api.addRow();
            });
            $(document).on('click', '.delRow',function(){
                Controller.api.delRow($(this));
            });

        },
        edit: function () {
            Controller.api.bindevent();
            $(document).on('click', '.addRow',function(){
                Controller.api.addRow();
            });
            $(document).on('click', '.delRow',function(){
                Controller.api.delRow($(this));
            });
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $("input[type=radio][name='row[config]']").change(function(){
                    var type=$(this).val();
                    if(type==1){
                        $('.table-responsive1').show();
                    }else{
                        $('.table-responsive1').hide();
                    }
                });
		
                //bootstrap-select 选中/取消选中 事件
                $('.selectpicker').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
                    var length=$(this).closest('tr').find('td').eq(0).html();
                    Controller.api.addselectpage($(this).closest('tr').find('td').eq(2),e.target.value,length);
                })
            },
            addRow:function (data) {
                var type="";
                var admin_ids="";
                if (data&&Object.keys(data).length > 0){
                    type=data.type;
                    admin_ids=data.admin_ids;
                }

                var len = $('#table tr').length;
                var lens = len - 2;
                var tempTr = '<tr>' +
                    '<td>' + len + '</td>' +
                    '<td>' +
                    '<select class="form-control selectpicker" data-rule="required" name="step['+len+'][type]" data-value="2">' +
					'<option value="2">指定人员</option>' +
                    '<option value="1">上级主管</option>' +
					'<option value="3">指定部门</option>' +
                    '</select>' +
                    '</td>' +

                    '<td>' +
                    '<input   data-source="facrm/common/selectpage/model/admin/type/all" data-field="nickname"' +
                    'class="form-control selectpage" name="step['+len+'][admin_ids]" type="text" value="" data-multiple="true" value="'+admin_ids+'">' +
                    '</td>' +

                    '<td><button type="button" class="btn btn-default delRow"  >删除</button> </td>' +
                    '</tr>';
                $('#table tbody tr:eq(' + lens + ')').after(tempTr);
                index++;
                $(function () {
                    Controller.api.bindevent();
                });
            },
            // 删除
            delRow:function(obj) {
                if ($('#table tbody tr').length == 1) {
                    alert(__("Can't delete"));
                    return false;
                }
                $(obj).parent().parent().remove();
			},
			addselectpage:function (obj,type,length) {
                var url="facrm/common/selectpage/model/admin/type/all";
                var field="nickname";
                var tempTr='';
                if (type==3){
                    url="facrm/common/group";
                    field="name";
                }
                var tempTr ='<input   data-source="'+url+'" data-field="'+field+'"' +
                    'class="form-control selectpage" name="step['+length+'][admin_ids]" type="text" value="" data-multiple="true" value="">';
                obj.html(tempTr);
                $(function () {
                  Controller.api.bindevent();
                });
            }
        }
    };
    return Controller;
});


